package sstable

import (
	"io/ioutil"
	"log"
	"lsmtree/config"
	"path"
	"sync"
	"time"
)

var levelMaxSize []uint32

func (tree *TableTree) Init(dir string) {
	log.Println("The SSTable list are being loaded")
	start := time.Now()

	defer func() {
		elapse := time.Since(start)
		log.Println("The SSTable list are loaded, cost time:", elapse)
	}()

	con := config.GetConfig()
	// 初始化每一层文件的最大值
	levelMaxSize = make([]uint32, 10)

	levelMaxSize[0] = con.Level0Size
	for i := 1; i < 9; i++ {
		levelMaxSize[i] = levelMaxSize[i-1] * 10
	}

	tree.levels = make([]*TableNode, 10)
	tree.lock = &sync.RWMutex{}
	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		log.Println("Failed to read the database file")
		panic(err)
	}

	for _, info := range infos {
		// 如果是 sstable 文件
		if path.Ext(info.Name()) == ".db" {

		}
	}

}
